package com.lsj.gulimall.product.service.impl;

import com.lsj.common.constant.ProductConstant;
import com.lsj.gulimall.product.dao.*;
import com.lsj.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.lsj.gulimall.product.entity.AttrGroupEntity;
import com.lsj.gulimall.product.entity.CategoryEntity;
import com.lsj.gulimall.product.service.AttrGroupService;
import com.lsj.gulimall.product.service.CategoryService;
import com.lsj.gulimall.product.vo.AttrListVo;
import com.lsj.gulimall.product.vo.AttrResVo;
import com.lsj.gulimall.product.vo.AttrVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lsj.common.utils.PageUtils;
import com.lsj.common.utils.Query;

import com.lsj.gulimall.product.entity.AttrEntity;
import com.lsj.gulimall.product.service.AttrService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.annotation.Resources;


@Service("attrService")
@Slf4j
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Resource
    AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Autowired
    AttrGroupService attrGroupService;

    @Resource
    AttrGroupDao attrGroupDao;

    @Resource
    CategoryDao categoryDao;

    @Resource
    AttrDao attrDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, String type, Long catId) {
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        if (catId != 0) {
            wrapper.eq("catelog_id",catId);
        }
        if (!StringUtils.isEmpty(type)) {
            if (ProductConstant.AttrEnum.ATTR_TYPE_BASE.getMsg().equals(type)) {
                wrapper.eq("attr_type",ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode());
            } else if (ProductConstant.AttrEnum.ATTR_TYPE_SALE.getMsg().equals(type)) {
                wrapper.eq("attr_type",ProductConstant.AttrEnum.ATTR_TYPE_SALE.getCode());
            }
        }
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            wrapper.like("attr_name",key);
        }
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                wrapper);
        PageUtils pageUtils = new PageUtils(page);
        // 设置分类和分组名称
        List<AttrEntity> records = page.getRecords();
        List<AttrListVo> attrList = records.stream().map(attrEntity -> {
            AttrListVo attrListVo = new AttrListVo();
            BeanUtils.copyProperties(attrEntity,attrListVo);
            if (ProductConstant.AttrEnum.ATTR_TYPE_BASE.getMsg().equalsIgnoreCase(type)) {
                AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = attrAttrgroupRelationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
                if (attrAttrgroupRelationEntity != null && attrAttrgroupRelationEntity.getAttrGroupId() != null) {
                    AttrGroupEntity attrGroupEntity = attrGroupDao.selectOne(new QueryWrapper<AttrGroupEntity>().eq("attr_group_id", attrAttrgroupRelationEntity.getAttrGroupId()));
                    log.info("attrGroupEntity的值是多少啊{}"+attrGroupEntity);
                    attrListVo.setGroupName(attrGroupEntity.getAttrGroupName());
                }
            }
            CategoryEntity categoryEntity = categoryDao.selectOne(new QueryWrapper<CategoryEntity>().eq("cat_id", attrEntity.getCatelogId()));
            if (categoryEntity != null) {
                attrListVo.setCatelogName(categoryEntity.getName());
            }
            return attrListVo;
        }).collect(Collectors.toList());
        pageUtils.setList(attrList);
        return pageUtils;
    }

    /**
     * 保存属性和属性分组关系表
     * @param attrVo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveAttrDetail(AttrVo attrVo) {
        // 保存属性表
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attrVo,attrEntity);
        this.baseMapper.insert(attrEntity);
        if (attrVo.getAttrGroupId() != null) {
            // 保存属性分组表 attrAttrgroupRelationDao
            AttrAttrgroupRelationEntity attrAttrgroupRelation = new AttrAttrgroupRelationEntity();
            attrAttrgroupRelation.setAttrId(attrEntity.getAttrId());
            attrAttrgroupRelation.setAttrGroupId(attrVo.getAttrGroupId());
            attrAttrgroupRelation.setAttrSort(0);
            attrAttrgroupRelationDao.insert(attrAttrgroupRelation);
        }
    }

    @Override
    public AttrResVo getAttrDetail(Long attrId) {
        AttrResVo attrResVo = new AttrResVo();
        AttrEntity attrEntity = this.baseMapper.selectById(attrId);
        if (attrEntity != null) {
            BeanUtils.copyProperties(attrEntity,attrResVo);
        }
        // 获取属性分组关系
        QueryWrapper<AttrAttrgroupRelationEntity> wrapper = new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id",attrId);
        AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = attrAttrgroupRelationDao.selectOne(wrapper);
        if (attrAttrgroupRelationEntity != null) {
            attrResVo.setAttrGroupId(attrAttrgroupRelationEntity.getAttrGroupId());
        }
        // 获取完整的三级分类路径
        List<Long> categroyIds = new ArrayList<>();
        Long[] categroyPath = attrGroupService.getCategroyIds(attrEntity.getCatelogId(), categroyIds);
        if (categroyIds.size() > 0) {
            attrResVo.setCatelogPath(categroyPath);
        }
        return attrResVo;
    }

    /**
     * 保存属性和属性分组关系表
     * @param attrVo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateAttrDetail(AttrVo attrVo) {
        // 更新属性表
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attrVo,attrEntity);
        this.baseMapper.updateById(attrEntity);

        // 更新属性分组关系表
        if (attrVo.getAttrGroupId() != null) {
            Long attrId = attrVo.getAttrId();
            QueryWrapper<AttrAttrgroupRelationEntity> wrapper = new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id",attrId);
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = attrAttrgroupRelationDao.selectOne(wrapper);
            if (attrAttrgroupRelationEntity != null) {
                attrAttrgroupRelationEntity.setAttrGroupId(attrVo.getAttrGroupId());
                attrAttrgroupRelationDao.updateById(attrAttrgroupRelationEntity);
            } else {
                AttrAttrgroupRelationEntity attrAttrgroupRelation = new AttrAttrgroupRelationEntity();
                attrAttrgroupRelation.setAttrId(attrEntity.getAttrId());
                attrAttrgroupRelation.setAttrGroupId(attrVo.getAttrGroupId());
                attrAttrgroupRelation.setAttrSort(0);
                attrAttrgroupRelationDao.insert(attrAttrgroupRelation);
            }
        }
    }

    @Override
    public List<Long> selectSearchAttrs(List<Long> attrIds) {
        List<Long> searchAttrIds = attrDao.selectSearchAttrs(attrIds);
        return searchAttrIds;
    }

}